package Code.Test;

import javax.print.DocFlavor;
import java.util.Scanner;

//键盘录入一个字符串，要求长度小于9，只能是数字，将内容变成罗马数字
//罗马数字没有0，如果有零转化为""
public class Test1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入一个字符串：");
        String str = scanner.next();
        if (str.length() <= 9) {
            System.out.println(TransitionTwo(str));
        }else {
            System.out.println("错误！长度只能为9");
        }

    }

    public static String Transition(String str) {
        String[] rome = {"","Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"};

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char temp = str.charAt(i);
            if (temp >= '0' && temp <= '9') {
                int index = temp - 48;
                sb.append(rome[index]);
            }else {
                return "错误！请输入数字";
            }
        }
        return sb.toString();
    }

    //方法二
    public static String TransitionTwo(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char temp = str.charAt(i);

            if (temp >= '0' && temp <= '9') {
                String s = switch (temp){
                    case '1'->"Ⅰ";
                    case '2'->"Ⅱ";
                    case '3'->"Ⅲ";
                    case '4'->"Ⅳ";
                    case '5'->"Ⅴ";
                    case '6'->"Ⅵ";
                    case '7'->"Ⅶ";
                    case '8'->"Ⅷ";
                    case '9'->"Ⅸ";
                    default -> "";
                };
                sb.append(s);
            }else {
                return "错误！请输入数字";
            }
        }
        return sb.toString();
    }
}
